home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Misc / emu / fbzx.lha / fbzx / microdrive.c < prev    next >
C/C++ Source or Header  |  2004-01-06  |  4KB  |  156 lines

  1. #include "microdrive.h"
  2. #include "computer.h"
  3. #include "emulator.h"
  4.  
  5. byte basura;
  6.  
  7. void microdrive_init() {
  8.  
  9.     int bucle;
  10.     
  11.     basura = 0;
  12.     
  13.     ordenador.mdr_active = 1;
  14.     ordenador.mdr_paged = 0;
  15.     
  16.     for(bucle=0;bucle<137922;bucle++)
  17.         ordenador.mdr_cartridge[bucle]=0xFF; // cartridge erased
  18.     ordenador.mdr_cartridge[137922]=0; // but not write-protected
  19.  
  20.     ordenador.mdr_tapehead=0;
  21.     ordenador.mdr_drive=0; // no motor on
  22.     ordenador.mdr_old_STATUS=0x00; // default -> no down edge
  23.     ordenador.mdr_modified=0; // not modified
  24.     ordenador.mdr_current_mdr[0]=0; // no cartridge
  25.     
  26. }
  27.  
  28. void microdrive_reset() {
  29.     
  30.     ordenador.mdr_gap = 15;
  31.     ordenador.mdr_nogap = 15;
  32.     ordenador.mdr_tapehead = 0; // head is at start position    
  33.     
  34. }
  35.  
  36. void microdrive_emulate(int tstados) {
  37.     // still nothing to do here
  38.     
  39. }
  40.  
  41. byte microdrive_in(word Port) {
  42.  
  43.     byte retorno;
  44.  
  45.     /* allow access to the port only if motor 1 is ON and there's a file open */
  46.     
  47.     if(((Port|0xFFE7)==0xFFE7)&&(ordenador.mdr_drive==0x01)&&(ordenador.mdr_current_mdr[0])) {
  48.         if(ordenador.mdr_bytes<ordenador.mdr_maxbytes) {
  49.             retorno=ordenador.mdr_cartridge[ordenador.mdr_tapehead];
  50.             basura=retorno;
  51.             increment_head();
  52.         } else {
  53.             retorno = basura;            
  54.         }
  55.         ordenador.mdr_bytes++;
  56.         return (retorno);
  57.     }
  58.     
  59.     if((Port|0xFFE7)==0xFFEF) {
  60.         if((ordenador.mdr_drive==0x01)&&(ordenador.mdr_current_mdr[0])) { // motor 1 ON and file selected
  61.             if(ordenador.mdr_gap) {
  62.                 retorno=0xFE; // GAP and SYNC high
  63.                 ordenador.mdr_gap--;
  64.             } else {
  65.                 retorno=0xF8; // GAP and SYNC low
  66.                 if(ordenador.mdr_nogap)
  67.                     ordenador.mdr_nogap--;
  68.                 else {
  69.                     ordenador.mdr_gap=15;
  70.                     ordenador.mdr_nogap=15;
  71.                 }
  72.             }
  73.             if(!ordenador.mdr_cartridge[137922]) // if write protected
  74.                 retorno|=0x01; // active bit
  75.         } else // motor 1 OFF
  76.             retorno=0xFF;
  77.  
  78.         microdrive_restart();
  79.         return (retorno);
  80.     }
  81.  
  82.     if ((Port|0xFFE7)==0xFFF7) {        
  83.         microdrive_restart();        
  84.         return (0xFF);
  85.     }
  86.     
  87.     return(0xFF);
  88.     
  89. }
  90.  
  91. void microdrive_out(word Port,byte Value) {
  92.  
  93.     /* allow access to the port only if motor 1 is ON and there's a file open */
  94.     
  95.     if(((Port|0xFFE7)==0xFFE7)&&(ordenador.mdr_drive==0x01)&&(ordenador.mdr_current_mdr[0])) {
  96.         if((ordenador.mdr_bytes>11)&&(ordenador.mdr_bytes<(ordenador.mdr_maxbytes+12))) {
  97.             ordenador.mdr_cartridge[ordenador.mdr_tapehead]=(unsigned int) Value;            
  98.             increment_head();
  99.             ordenador.mdr_modified=1;
  100.         }
  101.         ordenador.mdr_bytes++;
  102.         return;
  103.     }
  104.     
  105.     if((Port|0xFFE7)==0xFFEF) {
  106.         if(((Value&0x02)==0)&&((ordenador.mdr_old_STATUS&0x02)==2)) { // edge down-> new bit for motor ON
  107.             ordenador.mdr_drive=((ordenador.mdr_drive<<1)&0xFE); // rotate one drive
  108.             if(!(Value&0x01)) // if COM DATA is 0, we add a 1 bit to mdr_drive
  109.                 ordenador.mdr_drive|=0x01;
  110.             
  111.             if(ordenador.mdr_modified) { // if the cartridge has been modified, we store it in hard disk
  112.                 ordenador.mdr_file=fopen(ordenador.mdr_current_mdr,"wb"); // create for write                
  113.                 if(ordenador.mdr_file==NULL) {
  114.                     sprintf(ordenador.osd_text,"Can't store the cartridge");
  115.                     ordenador.osd_time=150;
  116.                 } else {
  117.                     fwrite(ordenador.mdr_cartridge,137923,1,ordenador.mdr_file); // save cartridge
  118.                     fclose(ordenador.mdr_file);
  119.                     ordenador.mdr_file=NULL;
  120.                     ordenador.mdr_modified=0;
  121.                 }
  122.             }
  123.         }
  124.         ordenador.mdr_old_STATUS=Value;
  125.         microdrive_restart();
  126.         return;
  127.     }
  128.  
  129.     if ((Port|0xFFE7)==0xFFF7) {
  130.         microdrive_restart();
  131.         return;
  132.     }
  133. }
  134.  
  135. void increment_head() { // gets the tape head to the next byte
  136.     
  137.     ordenador.mdr_tapehead++;
  138.     if(ordenador.mdr_tapehead>137921)
  139.         ordenador.mdr_tapehead=0;    
  140. }
  141.  
  142. void microdrive_restart() { // there's an access to a port. Reset counters and relocate the head
  143.  
  144.     //printf("Inicializado\n");
  145.     
  146.     while(((ordenador.mdr_tapehead%543)!=0)&&((ordenador.mdr_tapehead%543)!=15))
  147.         increment_head(); // put head in the start of a block
  148.     
  149.     ordenador.mdr_bytes = 0; // reset current number of bytes written
  150.     if((ordenador.mdr_tapehead%543)==0)
  151.         ordenador.mdr_maxbytes = 15; // up to 15 bytes for header blocks
  152.     else
  153.         ordenador.mdr_maxbytes = 528; // up to 528 bytes for data blocks        
  154.     
  155. }
  156.